package com.t.bricks.bussiness.db.dao.login.impl;

import java.util.List;
import java.util.Properties;

import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.t.bricks.bussiness.db.dao.login.LoginDao;
import com.t.bricks.bussiness.db.mapper.login.LoginMapper;
import com.t.bricks.bussiness.model.login.LoginUser;
import com.t.bricks.model.Log;
import com.t.bricks.model.MsgEmity;

@Repository
public class LoginDaoImpl implements LoginDao {
	
	@Autowired
	private LoginMapper loginMapper;
	
	@Autowired
    private SqlSessionFactory sqlSessionFactory;
	
	/**
	 * 是否使用dev库, 0:未检测;1:是;2:否
	 */
	private int iDev = 0;
	
	/**
	 * 按uId查询用户信息
	 * @param uId
	 * @return
	 */
	@Override
	public MsgEmity findById(String uId) {
		LoginUser loginUser = null;
		try {
			loginUser = loginMapper.findById(uId);
		} catch (Exception e) {
			Log.error("查询时发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败!",7001);
		}
		
		if (null == loginUser) {
			Log.debug("该用户不存在!");
			return new MsgEmity(false, "该用户不存在!",7002);
		}
		
		return new MsgEmity(true, "查询成功!", loginUser);
	}
	
	/**
	 * 取用户名或工号对应的用户集合
	 * @param sNameOrNo
	 * @param sOwner
	 * @return
	 */
	@Override
	public MsgEmity findByNameOrNo(String sNameOrNo, String sOwner) {
		List<LoginUser> list = null;
		try {
			list = loginMapper.findByNameOrNo(sNameOrNo, sOwner);
		} catch (Exception e) {
			Log.error("查询时发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败!",7001);
		}
		
		return new MsgEmity(true, "查询成功!", list);
	}

	/**
	 * 根据编号查询业务用户数据
	 * @param uId
	 * @param sOwner 用户来源
	 * @return
	 */
	@Override
	public MsgEmity findBussinessUserById(String uId, String sOwner) {
		if (iDev == 0) {
			Configuration configuration = sqlSessionFactory.getConfiguration();
			Properties variables = configuration.getVariables();
			
			String dbName = variables.getProperty("BricksBaseSystem");//查配置文件中的变量'mybatis.configuration.variables.BricksBaseSystem'
			if (null == dbName || "".equals(dbName)) {
				return new MsgEmity(false, "'用户来源库'格式错误,需为'库名.表名'", 7001);
			}
			
			iDev = (dbName.toUpperCase().startsWith("DEV_")) ? 1 : 2;
		}
		
		sOwner = null == sOwner || "".equals(sOwner.trim()) ? "BricksBaseSystem.LoginUser" : sOwner;
		String[] array = sOwner.split(".");
		if (array.length != 2) {
			return new MsgEmity(false, "'用户来源'格式错误,需为'库名.表名'", 7001);
		}
		
		if (iDev == 1 && !sOwner.toUpperCase().startsWith("DEV_")) {//使用开发库
			sOwner = String.format("Dev_%s", sOwner);
		}
		
		LoginUser loginUser = null;
		try {
			loginUser = loginMapper.findBussinessUserById(uId, sOwner);
		} catch (Exception e) {
			Log.error("查询时发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败!",7001);
		}
		
		if (null == loginUser) {
			Log.debug("该用户不存在!");
			return new MsgEmity(false, "该用户不存在!",7002);
		}
		
		return new MsgEmity(true, "查询成功!", loginUser);
	}
	
	/**
	 * 取识别号对应的业务用户数据
	 * @param sNameOrNo 用户名或识别号
	 * @param sOwner 用户来源
	 * @return
	 */
	@Override
	public MsgEmity findBussinessUserByNameOrNo(String sNameOrNo, String sOwner) {
		if (iDev == 0) {
			Configuration configuration = sqlSessionFactory.getConfiguration();
			Properties variables = configuration.getVariables();
			
			String dbName = variables.getProperty("BricksBaseSystem");//查配置文件中的变量'mybatis.configuration.variables.BricksBaseSystem'
			if (null == dbName || "".equals(dbName)) {
				return new MsgEmity(false, "'用户来源库'格式错误,需为'库名.表名'", 7001);
			}
			
			iDev = (dbName.toUpperCase().startsWith("DEV_")) ? 1 : 2;
		}
		
		sOwner = null == sOwner || "".equals(sOwner.trim()) ? "BricksBaseSystem.LoginUser" : sOwner;
		String[] array = sOwner.split(".");
		if (array.length != 2) {
			return new MsgEmity(false, "'用户来源'格式错误,需为'库名.表名'", 7001);
		}
		
		if (iDev == 1 && !sOwner.toUpperCase().startsWith("DEV_")) {//使用开发库
			sOwner = String.format("Dev_%s", sOwner);
		}
		
		List<LoginUser> list = null;
		try {
			list = loginMapper.findBussinessUserByNameOrNo(sNameOrNo, sOwner);
		} catch (Exception e) {
			Log.error("查询时发生异常:", e.getMessage());
			return new MsgEmity(false, "查询失败!",7001);
		}
		
		return new MsgEmity(true, "查询成功!", list);
	}
	
}
